/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.domain.QiniuContent;
import me.zhengjie.modules.cspimp.psp.domain.Accident;
import me.zhengjie.modules.cspimp.psp.service.dto.AccidentDto;
import me.zhengjie.modules.system.domain.AreaManage;
import me.zhengjie.modules.system.domain.DocManage;
import me.zhengjie.repository.QiniuContentRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.DocManageRepository;
import me.zhengjie.modules.system.service.DocManageService;
import me.zhengjie.modules.system.service.dto.DocManageDto;
import me.zhengjie.modules.system.service.dto.DocManageQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.DocManageMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 黄星星
 * @website https://docs.auauz.net
 * @description 服务实现
 * @date 2020-05-30
 **/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DocManageServiceImpl implements DocManageService {

    private final DocManageRepository docManageRepository;
    private final DocManageMapper docManageMapper;
    private final QiniuContentRepository qiniuContentRepository;

    @Override
    public Map<String, Object> queryAll(DocManageQueryCriteria criteria, Pageable pageable) {
        List<DocManage> page = docManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        List<DocManageDto> docManageList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page)) {
            for (DocManage docManage : page) {
                DocManageDto docManageDto = new DocManageDto();
                BeanUtils.copyProperties(docManage, docManageDto);
                QiniuContent qiniuContent =qiniuContentRepository.findById(docManage.getDocFileId()).orElse(null);
                if (null != qiniuContent) {
                    docManageDto.setFilename(qiniuContent.getKey());
                    docManageDto.setSize(qiniuContent.getSize());
                    docManageDto.setSuffix(qiniuContent.getSuffix());
                }
                docManageList.add(docManageDto);
            }
        }
        return PageUtil.toPage(PageUtil.listPage(docManageList, pageable));
    }

    @Override
    public List<DocManageDto> queryAll(DocManageQueryCriteria criteria) {
        return docManageMapper.toDto(docManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public DocManageDto findById(Long id) {
        DocManage docManage = docManageRepository.findById(id).orElseGet(DocManage::new);
        ValidationUtil.isNull(docManage.getId(), "DocManage", "id", id);
        return docManageMapper.toDto(docManage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DocManageDto create(DocManage resources) {
        resources.setCid(0l);
        return docManageMapper.toDto(docManageRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DocManage resources) {
        DocManage docManage = docManageRepository.findById(resources.getId()).orElseGet(DocManage::new);
        ValidationUtil.isNull(docManage.getId(), "DocManage", "id", resources.getId());
        docManage.copy(resources);
        docManageRepository.save(docManage);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            docManageRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DocManageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DocManageDto docManage : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("创建时间", docManage.getCreateTime());
            map.put("更新时间", docManage.getUpdateTime());
            map.put("创建者", docManage.getCreateBy());
            map.put("更新者", docManage.getUpdateBy());
            map.put("文档标题", docManage.getDocTitle());
            map.put("文档类型code", docManage.getDocTypeCode());
            map.put("文档关键词", docManage.getDocKeyWord());
            map.put("文档资料", docManage.getFilename());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}